package ee.ts022.tuum.services.ws.endpoints;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import ee.ts022.tuum.domain.CstAddress;
import ee.ts022.tuum.services.CustomerAddressService;
import ee.ts022.tuum.services.ws.generated.DeleteCustomerAddress;
import ee.ts022.tuum.services.ws.generated.DeleteCustomerAddressResponse;
import ee.ts022.tuum.services.ws.generated.GetCustomerAddresses;
import ee.ts022.tuum.services.ws.generated.GetCustomerAddressesResponse;
import ee.ts022.tuum.services.ws.generated.InsertCustomerAddress;
import ee.ts022.tuum.services.ws.generated.InsertCustomerAddressResponse;
import ee.ts022.tuum.services.ws.generated.SaveCustomerAddress;
import ee.ts022.tuum.services.ws.generated.SaveCustomerAddressResponse;
import ee.ts022.tuum.services.ws.utils.DomainSoapParser;
import ee.ts022.tuum.services.ws.utils.SoapDomainParser;

@Endpoint
public class CustomerAddressServiceEndpoint {
	
	public static final String NAMESPACE_URI = "http://ts022.ee";
	private static final Logger logger = LoggerFactory.getLogger(CustomerAddressServiceEndpoint.class);
	
	@Autowired
	CustomerAddressService customerAddressService;
	
	@PayloadRoot(localPart = "getCustomerAddresses", namespace = NAMESPACE_URI)
	@ResponsePayload
	public GetCustomerAddressesResponse getCustomerAddressesResponse(@RequestPayload GetCustomerAddresses getCustomerAddresses) {
		logger.info("Getting customer {} addresses",getCustomerAddresses.getCustomer());
		
		GetCustomerAddressesResponse response = new GetCustomerAddressesResponse();
		for (ee.ts022.tuum.domain.CstAddress cstAddress : customerAddressService.getCustomerAddresses(getCustomerAddresses.getCustomer())) {
			logger.info("domain cstAadress {} type is {} ",cstAddress.getCstAddress(), cstAddress.getAddressType());
			response.getAddresses().add(DomainSoapParser.parseCstAddress(cstAddress));
		}
		return response;
	}
	
	@PayloadRoot(localPart = "deleteCustomerAddress", namespace = NAMESPACE_URI)
	@ResponsePayload
	public DeleteCustomerAddressResponse deleteCustomerAddressResponse(@RequestPayload DeleteCustomerAddress deleteCustomerAddress) {
		logger.info("Deleting customer address {}",deleteCustomerAddress.getAddress());
		
		DeleteCustomerAddressResponse response = new DeleteCustomerAddressResponse();
		CstAddress cstAddress = customerAddressService.getCstAddress(deleteCustomerAddress.getAddress());
		if(cstAddress == null || !customerAddressService.delete(cstAddress)){
			logger.error("Aadressi kustutamine ebaõnnestus");
			response.setReturnCode(1);
			response.setMessage("Aadressi kustutamine ebaõnnestus");
		}
		return response;
	}
	
	@PayloadRoot(localPart = "insertCustomerAddress", namespace = NAMESPACE_URI)
	@ResponsePayload
	public InsertCustomerAddressResponse insertCustomerAddressResponse(@RequestPayload InsertCustomerAddress insertCustomerAddress) {
		logger.info("Inserting new address {} for customer {}", insertCustomerAddress.getAddress().getAddress(), insertCustomerAddress.getAddress().getCustomer());
		
		InsertCustomerAddressResponse response = new InsertCustomerAddressResponse();
		if(!customerAddressService.insert(SoapDomainParser.parseCstAddress(insertCustomerAddress.getAddress()))){
			logger.error("Andmebaasi aadressi lisamine ebaõnnestus");
			response.setReturnCode(1);
			response.setMessage("Andmebaasi aadressi lisamine ebaõnnestus");
		}
		return response;
	}
	
	@PayloadRoot(localPart = "saveCustomerAddress", namespace = NAMESPACE_URI)
	@ResponsePayload
	public SaveCustomerAddressResponse saveCustomerAddressResponse(@RequestPayload SaveCustomerAddress saveCustomerAddress) {
		logger.info("saving address {} for customer {}", saveCustomerAddress.getAddress().getCstAddress(), saveCustomerAddress.getAddress().getCustomer());
		
		boolean success = true;
		SaveCustomerAddressResponse response = new SaveCustomerAddressResponse();
		CstAddress cstAddress = customerAddressService.getCstAddress(saveCustomerAddress.getAddress().getCstAddress());
		if(cstAddress != null) {
			ee.ts022.tuum.services.ws.generated.CstAddress c = saveCustomerAddress.getAddress();
			cstAddress.setAddress(c.getAddress());
			cstAddress.setAddressType(c.getAddressType());
			cstAddress.setCountry(c.getCountry());
			cstAddress.setHouse(c.getHouse());
			cstAddress.setTownCounty(c.getTownCounty());
			cstAddress.setZip(c.getZip());
			success = customerAddressService.save(cstAddress);

		} else {
			success = false;
		}
		
		if(!success) {
			logger.error("Andmebaasi salvestamine ebaõnnestus");
			response.setReturnCode(1);
			response.setMessage("Andmebaasi salvestamine ebaõnnestus");
		}
		return response;
	}
	
}
